<?
/*
    This collection of functions (libary) is licensed under GPL2.
    See LICENSE or www.gnu.org for more details.

    Author: Henri Ranki 19.9.2001

    Implements the glue between kone and other modules. Use this to transfer
    Itus to kone.

    $Id: dzglue.php 597 2006-04-03 16:15:46Z jyry $
*/
include_once("sodebug.php");
include_once("sogeneric.php");
include_once("sostring.php");

include_once("dzconst.php");


/** Send a request to Kone
 *  Decides whether to use Kone locally or use rpc. Rpc is defined in
 *  application ini file with key kone.
 *
 *  Parameters and result are carried in Itu
 *
 *  @param soSet $Itu Itu to send to Kone
 *  @return dzItu result returned from Kone
 */
function sendRequest($Itu) {
    soDebug("dzGlue: Sending Itu to Kone", SOD_LO);
    /* Read in the kone definition from application ini 
       kone is defined with keyword kone and the format is
       as follows: kone=host,directory,port
       */
    $kone_def = dzGetIniItem("general", "kone", "app");

    if ($kone_def != "")
        /* call kone via rpc */
        $result = sendRequestRpc($kone_def, $Itu);
    else {
        /* Include the code for dzKone-class */
        include_once("dzkone.php");
        /* Call kone locally */
        $kone = new dzKone();
        $result = $kone->request($Itu);
    } // if $kone_def

    return $result;
} // sendRequest


/** Send a request to Kone using rpc
 *  Sends the given Itu to specified Kone. Rpc info is specified with comma
 *  separated string. Rpc info defines the host and directory where Kone
 *  is located. In addition to that the port is defined.
 *
 *  @param string $RpcInfo Comma separated list of rpc information
 *  @param dzitu $Itu Itu to send to Kone
 *  @return dzItu Result from Kone
 */
function sendRequestRpc($RpcInfo, $Itu) {
    soDebug("dzGlue: Doing rpc. Info=$RpcInfo", SOD_LO);
    global $soDebugLevel;
    /* Encode request that will be sent */
    $encoded_itu = $Itu->encode();

    /* Both spaces and commas are accepted as delimiter */
    $RpcInfo = soSwapStr($RpcInfo, ",", " ");
    $kone_arr = soExplode($RpcInfo, " ");

    /* Check that path to kone starts with a slash. This is strange feature
       in xmlrpc library. The path must start with slash. */
    if (substr($kone_arr[1], 0 ,1) != "/") $kone_arr[1] = "/".$kone_arr[1];

    /* Add the name of the file where Kone is implemented to the
       path to Kone */
    $kone_arr[1] = soJoinStr($kone_arr[1], "/", DZ_REQUEST_ACCESS_POINT);

    /* Create rpc packet that carries the parameters to the rpc server */
    $rpc_msg = new xmlrpcmsg('serve_xml',
                			 array(new xmlrpcval($encoded_itu, "string")));
    /* kone_array is an array that contains needed information for rpc call
       Items are as follows: 0 - host name, 1 - directory and 2 - port */
    $rpc_client = new xmlrpc_client($kone_arr[1], $kone_arr[0], $kone_arr[2]);
    
    /* Turn debug on if atleast detailed debug level is configured */
    if ($soDebugLevel > SOD_DET)
        $rpc_client->setDebug(1);
    else
        $rpc_client->setDebug(0);
    /* Finally send request to Kone using xmlrpc */
    $rpc_result = $rpc_client->send($rpc_msg);

    if (soGetClass($rpc_result) != "xmlrpcresp") {
        soDebug("dzGlue: xmlrpc returned bad result. Rpc failed", SOD_HI_E);
        $result_itu = new dzItu();
        $result_itu->setBlockItem(DZI_CONTROL, DZK_RPC_ERROR, 666);
        $result_itu->setBlockItem(DZI_CONTROL, DZK_RPC_RESULT_MSG,
                                  "xmlrpc-library failed. Returned bad result.");
        return $result_itu;
    } // if (soGetClass($rpc_result)

    if (!$rpc_result->faultCode()) {
        $result_msg = $rpc_result->value();
    	$result = $result_msg->scalarval();
    	/* Decode the received string to itu */
        $result_itu = new dzItu($result);
    } else {
        soDebug("dzGlue: rpc call failed ".$rpc_result->faultCode().": ".
                $rpc_result->faultString(), SOD_HI_E);
        $result_itu = new dzItu();
        $result_itu->setBlockItem(DZI_CONTROL, DZK_RPC_ERROR,
                                  $rpc_result->faultCode());
        $result_itu->setBlockItem(DZI_CONTROL, DZK_RPC_RESULT_MSG,
                                  $rpc_result->faultString());
    } // if $rpc_result
    
    return $result_itu;
} // sendRequestRpc


/** Check Itu for rpc error
 *  Call this function find out if there was in error in the rpc.
 *  @param dzItu $Itu Itu received as a result from the rpc
 *  @return bool True if there were an error
 */
function isRpcError($Itu) {
    /* Check that the rpc was successfull */
    $error = $Itu->getBlockItemAt(DZI_CONTROL, DZK_RPC_ERROR);
    return $error;
} // isRpcError


/** Returns the the error text for an Itu
 *  Use this function to get the plain text error description in case of
 *  rpc error
 *  @param dzItu $Itu Itu received as a result from the rpc
 *  @return string Error text
 */
function getRpcErrorText($Itu) {
    $error_code = $Itu->getBlockItemAt(DZI_CONTROL, DZK_RPC_ERROR);
    $fault_string = $Itu->getBlockItemAt(DZI_CONTROL, DZK_RPC_RESULT_MSG);
    return "($error_code) $fault_string";
} // getRpcErrorText

?>
